package org.rbac.serv;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.rbac.dao.BaseDao;
import org.rbac.vo.Action;
import org.springframework.aop.support.AopUtils;
import org.springframework.stereotype.Service;

@Service("CommonServices")
public class CommonServices {

	@Resource(name="rbacBaseDao")
	private BaseDao rbacBaseDao;

	
   

    public BaseDao getDao() {
        return rbacBaseDao;
    }

    public void setDao(BaseDao dao) {
        this.rbacBaseDao = dao;
    }

  /**
   * 加载定义的全局资源数据
   * @return
   * @throws Exception
   */
    public Map<String, Action> loadApplicationMappingData()throws Exception {
        System.out.println("isAopProxy:"+AopUtils.isAopProxy(this));
        System.out.println("isCglibProxy:"+AopUtils.isCglibProxy(this));//cglib
        System.out.println("isJdkDynamicProxy:"+ AopUtils.isJdkDynamicProxy(this));//jdk动态代理
        
        Map<String, Action> mapping = new HashMap<String, Action>();
        String sql = "select a_name \"AName\",a_url \"AUrl\",a_loging \"ALoging\" ,a_control \"AControl\" from permission_action where a_type<>1 and a_url <> ' ' and a_status =1 order by a_url";
        @SuppressWarnings("unchecked")
        List<Map<String,Object>> lis = (List<Map<String, Object>>) rbacBaseDao.executeSQLQuery(sql);
        Action a =null;
        for (int i = 0; i < lis.size(); i++) {
            a=new Action();
            BeanUtils.populate(a, lis.get(i));
            mapping.put(a.getAUrl(), a);
        }
        return mapping;
    }
    /**
     * 加载数据字典
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> loadDictionary()throws Exception {
        String hql="select new map(CName as key,CValue as value) from PubConst where CRetention=0";
        return (List<Map<String, Object>>) getDao().queryForList(hql);
    	
    }
 
}
